package yicheng.dinnertogetherapp;

import java.util.List;

import yicheng.dinnertogetherapp.R;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class AddFriendActivity extends Activity {
	RelativeLayout addFriend_layout;
	EditText searchFriend_phoneNumber_editText;
	Button searchFriend_button;

	List<String> domainNames;
	
	AmazonClientManager clientManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		clientManager = new AmazonClientManager();
		setContentView(R.layout.add_friend_view);
		overridePendingTransition(R.anim.activity_left_in,R.anim.activity_left_out);
		
		addFriend_layout = (RelativeLayout) findViewById(R.id.addFriend_layout);
		
		addFriend_layout.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				hideKeyboard();
				return false;
			}
		});
		
		
		searchFriend_phoneNumber_editText = (EditText) findViewById(R.id.searchFriend_phoneNumber_editText);
		
		searchFriend_button = (Button) findViewById(R.id.searchFriend_button);
		
		
		searchFriend_button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (searchFriend_phoneNumber_editText.getText().toString().length() < 1){
					Toast.makeText(getApplicationContext(), "Phone number can not be empty!", Toast.LENGTH_SHORT).show();
				}
				else{
					searchPhoneNumber();
				}
				
			}
		});
		
		
		
		
		
		
		
		
		
		
	}
	

	
	
	private void searchPhoneNumber(){
		/*Intent gotoSearchFriendListActivity = new Intent("com.example.dinnertogetherapp.SEARCHFRIENDLISTACTIVITY");
		startActivity(gotoSearchFriendListActivity);*/
		/*new loadDataBase().execute();
		
		if (domainNames.contains(searchFriend_phoneNumber_editText.getText().toString())){
			
		}
		else{
			
		}*/
		
		
		Bundle data_bundle = new Bundle();
		data_bundle.putString("SerachedPhoneNumber", searchFriend_phoneNumber_editText.getText().toString());
	
		
		Intent gotoSearchFriendListActivity = new Intent("yicheng.dinnertogetherapp.SEARCHFRIENDLISTACTIVITY");
		gotoSearchFriendListActivity.putExtra("SearchedUserInformation", data_bundle);
		startActivity(gotoSearchFriendListActivity);
		finish();
		
	}
	
	
	
	
	
	private void hideKeyboard(){
		if(getCurrentFocus()!=null) {
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
		}
	}
	
	
	private class loadDataBase extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... Void) {
			// TODO Auto-generated method stu
			/*loadingSpinner.setVisibility(View.VISIBLE);*/
			domainNames = SimpleDB.getDomainNames(clientManager);


			//loadingSpinner.setVisibility(View.VISIBLE);


			/*	}
			catch (NullPointerException e){
				Toast.makeText(getApplicationContext(), "Your connection to Internet has problem, please check.", Toast.LENGTH_SHORT).show();
			}*/
	/*		Message msg = Message.obtain();
			msg.what = 1;
			handler.sendMessage(msg);*/
			//return null;
			return null;
		}
	}
	
	
	@Override
	public void onBackPressed() {
	    super.onBackPressed();
	    overridePendingTransition(R.anim.activity_right_in, R.anim.activity_right_out);
	}
	
	
}
